﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Gds.Model.ConfigModel.SerialPort
{
    public class EquipSerialPortModel : INotifyPropertyChanged
    {
        private int _portIndex;
        private int _totalLineNumber;
        private string _serialPortName;
        private string _boardRate;
        private int _waitTime;
        private int _sendWatiTime;
        private string _configType;
        /// <summary>
        /// 序号
        /// </summary>
        public int PortIndex
        {
            get
            {
                return _portIndex;
            }
            set
            {
                _portIndex = value;
                OnPropertyChanged("PortIndex");
            }
        }
        /// <summary>
        /// 分线号(相当于路数)
        /// </summary>
        public int TotalLineNumber
        {
            get
            {
                return _totalLineNumber;
            }
            set
            {
                _totalLineNumber = value;
                OnPropertyChanged("TotalLineNumber");
            }
        }
        /// <summary>
        /// 端口号
        /// </summary>
        public string SerialPortName
        {
            get
            {
                return _serialPortName;
            }
            set
            {
                _serialPortName = value;
                OnPropertyChanged("SerialPortName");
            }
        }

        /// <summary>
        /// 波特率
        /// </summary>
        public string BoardRate
        {
            get
            {
                return _boardRate;
            }
            set
            {
                _boardRate = value;
                OnPropertyChanged("BoardRate");
            }
        }
        /// <summary>
        /// 等待报文回复时间
        /// </summary>
        public int WaitTime
        {
            get
            {
                return _waitTime;
            }
            set
            {
                _waitTime = value;
                OnPropertyChanged("WaitTime");
            }
        }
        /// <summary>
        /// 发送间隔
        /// </summary>
        public int SendWatiTime
        {
            get
            {
                return _sendWatiTime;
            }
            set
            {
                _sendWatiTime = value;
                OnPropertyChanged("SendWatiTime");
            }
        }
        /// <summary>
        /// 用于区分控制器还有其他设备
        /// </summary>
        public string ConfigType
        {
            get
            {
                return _configType;
            }
            set
            {
                _configType = value;
                OnPropertyChanged("ConfigType");
            }
        }

        private ObservableCollection<ComboxSourceModel> _currentSerialportList;
        private ObservableCollection<ComboxSourceModel> _boardRateList;
        /// <summary>
        /// 串口下拉列表
        /// </summary>
        [XmlIgnore]
        public ObservableCollection<ComboxSourceModel> CurrentSerialportList
        {
            get
            {
                _currentSerialportList = new ObservableCollection<ComboxSourceModel>();
                var listPort = System.IO.Ports.SerialPort.GetPortNames();
                for (int i = 0; i < listPort.Length; i++)
                {
                    _currentSerialportList.Add(new ComboxSourceModel()
                    {
                        Key = i,
                        Value = listPort[i],
                        Type = "串口总集合",
                    });
                }
                return _currentSerialportList;
            }
        }
        /// <summary>
        ///波特率下拉列表
        /// </summary>
        [XmlIgnore]
        public ObservableCollection<ComboxSourceModel> BoardRateList
        {
            get
            {
                _boardRateList = new ObservableCollection<ComboxSourceModel>();
                #region boradRate list
                List<string> list_baud = new List<string>();
                list_baud.Add("1200,e,8,1");
                list_baud.Add("1200,n,8,1");
                list_baud.Add("2400,e,8,1");
                list_baud.Add("9600,e,8,1");
                list_baud.Add("9600,n,8,1");
                list_baud.Add("19200,e,8,1");
                list_baud.Add("19200,n,8,1");
                list_baud.Add("38400,e,8,1");
                list_baud.Add("38400,n,8,1");
                #endregion
                for (int i = 0; i < list_baud.Count; i++)
                {
                    _boardRateList.Add(new ComboxSourceModel
                    {
                        Key = i,
                        Value = list_baud[i],
                        Type = "波特率",
                    });
                }
                return _boardRateList;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

    }
}
